<script setup>
import { ref, onMounted } from "vue";
// import axios from "axios";
import { queryAllApi, addApi, queryByIdApi, updateApi, deleteByIdApi } from "@/api/dept";
import { ElMessage, ElMessageBox } from 'element-plus'

//查询部门
const deptList = ref([])

const search = async () => {
  // const result = await axios.get(`https://apifoxmock.com/m1/3128855-1224313-default/depts`)
  const result = await queryAllApi();
  if (result.code) {
    deptList.value = result.data;
  }
}
//钩子函数
onMounted(() => {
  search();
})


const dialogFormVisible = ref(false);
const formTitle = ref('');
const dept = ref({ name: '' });

//定义表单校验规则
const deptFormRef = ref();
const rules = ref({
  name: [
    { required: true, message: '部门名称为必填项', trigger: 'blur' },
    { min: 2, max: 10, message: '部门名称必须得在2-10位之间', trigger: 'blur' }
  ]
})

// 新增部门

const add = () => {
  // 重置校验信息  resetFields()
  if (deptFormRef.value) {
    deptFormRef.value.resetFields();
  }
  dialogFormVisible.value = true;
  formTitle.value = '新增部门';
  dept.value = { name: '' };
}

//保存部门,保存之前先校验
const save = async () => {
  if (!deptFormRef) return
  await deptFormRef.value.validate(async (valid, fields) => {

    let result;
    if (dept.value.id) {
      result = await updateApi(dept.value);
    } else {
      result = await addApi(dept.value)
    }
    if (valid) {//通过
      if (result.code) {
        ElMessage.success('保存成功');
        dialogFormVisible.value = false;
        search();
      } else {
        ElMessage.error(result.msg);
      }
    } else {//不通过
      ElMessage.error('表单校验不通过,请检查表单')
    }
  })
}
//编辑部门
const edit = async (id) => {
  const result = await queryByIdApi(id);
  if (result.code) {
    dialogFormVisible.value = true;
    dept.value = result.data;
    formTitle.value = '修改部门';
  }
}
//删除部门
const deleteByid = async (id) => {
  ElMessageBox.confirm(
    '您确认删除该部门吗?',
    'Warning',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async () => {
      const result = await deleteByIdApi(id);
      if (result.code) {
        ElMessage.success('删除部门成功');
        search();
      } else {
        ElMessage.error(result.msg)
      }
    })
    .catch(() => {
      ElMessage.info('操作已取消');
    })
}

</script>

<template>
  <h1>部门管理</h1>
  <div id="add">
    <el-button type="success" round @click="add">+新增部门</el-button>
  </div>
  <!-- 部门表格 -->
  <el-table :data="deptList" border style="width: 100%">
    <el-table-column type="index" label="序号" width="280" align="center" />
    <el-table-column prop="name" label="部门名称" width="280" align="center" />
    <el-table-column prop="createTime" label="最后操作时间" align="center" />
    <el-table-column prop="address" label="操作" width="280" align="center">
      <template #default="scope">
        <el-button size="small" type="primary" @click="edit(scope.row.id)">编辑</el-button>
        <el-button size="small" type="danger" @click="deleteByid(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 新增/修改部门 -->
  <el-dialog v-model="dialogFormVisible" :title="formTitle" width="500">
    <el-form :model="dept" ref="deptFormRef" :rules="rules">
      <el-form-item label="部门名称" label-width="100px" prop="name">
        <el-input v-model="dept.name" autocomplete="请输入部门名称，长度为2-10位" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确认</el-button>
      </div>
    </template>
  </el-dialog>



</template>


<style scoped>
#add {
  margin: 20px 0px;
}
</style>
